<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="候选人姓名">
              <a-input v-model="queryParam.candidateName" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addCandidate.add()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      showPagination="auto"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="$refs.editCandidate.edit(record)">编辑</a>
          <a-divider type="vertical" />
          <remove-candidate ref="removePoint" :candidateId="String(record.candidateId)" @ok="handleOk"/>
        </template>
      </span>
    </s-table>
    <add-candidate ref="addCandidate" :types="types" :subjects="subjects" @ok="handleOk" />
    <edit-candidate ref="editCandidate" :types="types" :subjects="subjects" @ok="handleOk"/>
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import { queryCandidate } from '@/api/vote/candidate'
import AddCandidate from '@/views/manage/modules/candidate/AddCandidate'
import RemoveCandidate from '@/views/manage/modules/candidate/RemoveCandidate'
import EditCandidate from '@/views/manage/modules/candidate/EditCandidate'
import { querySubject, queryType } from '@/api/vote/project'
// 用于处理过滤器动态获取数据
// let that

export default {
  name: 'PointList',
  components: {
    STable,
    moment,
    AddCandidate,
    RemoveCandidate,
    EditCandidate
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      types: [],
      subjects: [],
      // 表头
      columns: [
        {
          title: '候选人姓名',
          dataIndex: 'candidateName',
          align: 'center'
        },
        {
          title: '申报类型',
          dataIndex: 'type',
          align: 'center'
        },
        {
          title: '申报职务',
          dataIndex: 'subject',
          align: 'center'
        },
        {
          title: '单位',
          dataIndex: 'dept',
          align: 'center'
        },
        {
          title: '职工号',
          dataIndex: 'employeeNumber',
          align: 'center'
        },
        {
          title: '身份证号',
          dataIndex: 'idNumber',
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        return queryCandidate(Object.assign(parameter, queryJson))
          .then(res => {
            return res.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  // filters: {
  //   courseTextFilter (courseId) {
  //     let courseName = ''
  //     for (const course of that.courses) {
  //       if (parseInt(course.id) === parseInt(courseId)) {
  //         courseName = course.name
  //         break
  //       }
  //     }
  //     return courseName
  //   }
  // },
  beforeCreate () {
    // that = this
  },
  created () {
    this.tableOption()
    this.listSubject()
    this.listType()
    // listCourse()
    //   .then(res => {
    //     if (res.message !== 'error') {
    //       this.courses = res.result
    //     } else {
    //       this.$message.error('操作失败！')
    //     }
    //   })
    // listPoint()
    //   .then(res => {
    //     if (res.message !== 'error') {
    //       this.points = res.result
    //     } else {
    //       this.$message.error('操作失败！')
    //     }
    //   })
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    listType() {
      const that = this
      this.$nextTick().then(function () {
        const queryJson = {
          pageNo: 1,
          pageSize: 1000,
          queryParam: '{}'
        }
        queryType(queryJson).then(res => {
          for (let i = 0; i < res.result.data.length; i++) {
            that.types[i] = res.result.data[i].typeName
          }
        })
      })
    },
    listSubject () {
      const that = this
      this.$nextTick().then(function () {
        const queryJson = {
          pageNo: 1,
          pageSize: 1000,
          queryParam: '{}'
        }
        querySubject(queryJson).then(res => {
          for (let i = 0; i < res.result.data.length; i++) {
            that.subjects[i] = res.result.data[i].subjectName
          }
        })
      })
    },
    // handleEdit (record) {
    //   this.$refs.editPoint.edit(record)
    // },

    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>
